//
//  GRMessageView.m
//  Gree+
//
//  Created by 许金秧的IMAC  on 2017/7/3.
//  Copyright © 2017年 gree. All rights reserved.
//

#import "GRMessageView.h"


#define MARGIN 8

@implementation GRMessageView



- (UIImageView *)imageView
{
    if (!_imageView) {
        
        _imageView = [[UIImageView alloc] init];
        _imageView.clipsToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_imageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [_imageView setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisVertical)];
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.textColor = [UIColor whiteColor];
        _contentLabel.font = [UIFont systemFontOfSize:13];
        _contentLabel.numberOfLines = 0;
        _contentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH-MARGIN*10;
        [_contentLabel setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
        [_contentLabel setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisVertical)];
        [self addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {
    
    self.backgroundColor = [UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:0.8];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 5;
    
    
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MARGIN);
        make.centerX.equalTo(self.mas_centerX);
        
    }];
    
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8*3);
        make.top.equalTo(_imageView.mas_bottom).offset(MARGIN);
        make.right.mas_equalTo(-MARGIN*3);
        make.bottom.mas_equalTo(-MARGIN*2);
        make.width.greaterThanOrEqualTo(_imageView.mas_width);
    }];
}




- (void)updateConstraints {
    if (_imageView.image) {
        [_imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(30);
        }];
    } else {
        [_imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(0);
        }];
    }
    [super updateConstraints];
}



@end
